use std::sync::Arc;

use c_cim::{server::Server, user::User};
use tokio::{net::TcpListener, sync::Mutex};

#[tokio::main]
async fn main() {
    c_log::Logger::new().init();
    log::info!("server log inited");

    let server = Server::new("127.0.0.1:8888".to_owned());

    let mut cc_no = 1;
    let tcp_listner = TcpListener::bind(&server.addr).await.unwrap();
    let server_arc = Arc::new(Mutex::new(server));
    while let Ok((connection, _)) = tcp_listner.accept().await {
        log::info!("{}号用户注册", cc_no);
        let user = User::new(connection, cc_no.to_string(), server_arc.clone());
        log::info!("用户信息：{:?}", user);
        let mut server_lock = server_arc.lock().await;
        server_lock.add_user(cc_no.to_string(), user).await;
        log::info!("server: {:?}", server_lock);
        cc_no += 1;
    }
}
